package com.depth.viewer.request
{
	import com.depth.viewer.events.AppEvent;
	import com.depth.viewer.events.EventBus;
	import com.depth.viewer.facade.IocAppFacade;
	import com.depth.viewer.request.page.FZFPage;
	import com.depth.viewer.request.webservice.SingleWebService;
	import com.depth.viewer.utils.Hashtable;
	import com.depth.viewer.utils.Util;
	
	import flash.utils.Dictionary;
	
	import mx.rpc.soap.SOAPHeader;
	
	/**
	 * 连接池管理  WebService
	 * 连接池,启动的时候就创建连接
	 * 这样就避免了各种问题
	 * */
	public class ConnectionPoolUtil
	{
		
		private static var _instance:ConnectionPoolUtil;
		private var _hashTable:Hashtable = new Hashtable();
		/**
		 * 是否需要弹窗
		 * */
		private var _isPopUp:Boolean = true;
		private var _sumOfError:int = 0;
		private static const MAX:int = 10;
		
		public function ConnectionPoolUtil()
		{
			init();
		}
		
		public static function get instance():ConnectionPoolUtil{
			if(_instance==null){
				_instance = new ConnectionPoolUtil();
			}
			return _instance;
		}
		
		private function init():void{
			var dic:Dictionary = IocAppFacade.getInstance().getObject("webService") as Dictionary;
			var keys:Array  = Util.getObjectAttributes(dic);
			for each(var key:String in keys){
				var _request:SingleWebService = createRequest(key);
				_hashTable.add(key,_request);
			}
		}
		/**
		 * @param id 配置文件URL的ID
		 * @param resultHandler  返回的方法
		 * @param methodName 所要调用的方法名称
		 * @param args  参数列表
		 * */
		public function reuqest(id:String,resultHandler:Function,methodName:String,... args:Array):void{
			var pool:PoolResponder = new PoolResponder(resultHandler,faultFunction);
			if(!_hashTable.find(id)){
				var _request:SingleWebService = createRequest(id);
				_hashTable.add(id,_request);
			}
			(_hashTable.find(id) as SingleWebService).request(pool,methodName,args);
			
		}
		/**
		 * @param id 配置文件URL的ID
		 * @param resultHandler  返回的方法
		 * @param methodName 所要调用的方法名称
		 * @param args  参数列表
		 * */
		public function reuqestHeader(id:String,resultHandler:Function,methodName:String,header:SOAPHeader,... args:Array):void{
			var pool:PoolResponder = new PoolResponder(resultHandler,faultFunction);
			if(!_hashTable.find(id)){
				var _request:SingleWebService = createRequest(id);
				_hashTable.add(id,_request);
			}
			(_hashTable.find(id) as SingleWebService).request(pool,methodName,args,header);
			
		}
		/**
		 * 可以返回自带的data
		 * @param id 配置文件URL的ID
		 * @param resultHandler  返回的方法
		 * @param methodName 所要调用的方法名称
		 * @param data 需要传递的方法
		 * @param args  参数列表
		 * */
		public function reuqestData(id:String,resultHandler:Function,methodName:String,
								data:Object,... args:Array):void{
			var pool:PoolResponder = new PoolResponder(resultHandler,faultFunction);
			pool.data = data;
			pool.id = id;
			if(!_hashTable.find(id)){
				var _request:SingleWebService = createRequest(id);
				_hashTable.add(id,_request);
			}
			(_hashTable.find(id) as SingleWebService).request(pool,methodName,args);
			
		}
		
		
		
		private function faultFunction(e:Object,id:String):void{
			trace(e.fault.faultString);
			if(++_sumOfError>MAX&&_isPopUp){
				//停止所有的定时器
				EventBus.getInstance().dispatchEvent(new AppEvent(AppEvent.NETWORK_ERROR));
				new FZFPage().showErrorInfo();
				_isPopUp = false;
			}
			if(_hashTable.find(id)){
				var _request:SingleWebService = createRequest(id);
				_hashTable.reset(id,_request);
				trace("重置了一个连接--------------"+id);
			}
			/*setTimeout(function():void{
				IocAppFacade.sendNotification(ConstFacade.CANCEL_COMPLETE);
				ViewerSystemManager.getInstance().restart();
			},1000);*/
			
		}
		
		
		public function setIsPopUp():void{ 
			_isPopUp= true;
		}
		
		/**
		 *创建连接
		 * */
		private function createRequest(id:String):SingleWebService{
			var _serviceUrl:String  = IocAppFacade.getInstance().getObject("webService")[id];
			var _request:SingleWebService = new SingleWebService(_serviceUrl);
			trace("--------------------------------------创建一个新的连接"+id);
			return _request;
		}

		/**
		 * 错误次数
		 * */
		public function set sumOfError(value:int):void
		{
			_sumOfError = value;
		}

		
	}
}